using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class MaterialDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

		public MaterialDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the Material table.
		/// </summary>
		public void Insert(MaterialDTO material)
		{
			ValidationUtility.ValidateArgument("material", material);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@name", material.Name),
				new SqlParameter("@unit", material.Unit),
				new SqlParameter("@description", material.Description),
				new SqlParameter("@pricebyday", material.Pricebyday)
			};

			material.Id = (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "MaterialInsert", parameters);
		}

		/// <summary>
		/// Updates a record in the Material table.
		/// </summary>
		public void Update(MaterialDTO material)
		{
			ValidationUtility.ValidateArgument("material", material);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", material.Id),
				new SqlParameter("@name", material.Name),
				new SqlParameter("@unit", material.Unit),
				new SqlParameter("@description", material.Description),
				new SqlParameter("@pricebyday", material.Pricebyday)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MaterialUpdate", parameters);
		}

		/// <summary>
		/// Deletes a record from the Material table by its primary key.
		/// </summary>
		public void Delete(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "MaterialDelete", parameters);
		}

		/// <summary>
		/// Selects a single record from the Material table.
		/// </summary>
		public MaterialDTO Select(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MaterialSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the Material table.
		/// </summary>
		public string SelectJson(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "MaterialSelect", parameters);
		}

		/// <summary>
		/// Selects all records from the Material table.
		/// </summary>
		public List<MaterialDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "MaterialSelectAll"))
			{
				List<MaterialDTO> materialDTOList = new List<MaterialDTO>();
				while (dataReader.Read())
				{
					MaterialDTO materialDTO = MapDataReader(dataReader);
					materialDTOList.Add(materialDTO);
				}

				return materialDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the Material table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "MaterialSelectAll");
		}

		/// <summary>
		/// Creates a new instance of the MaterialDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private MaterialDTO MapDataReader(SqlDataReader dataReader)
		{
			MaterialDTO materialDTO = new MaterialDTO();
			materialDTO.Id = dataReader.GetInt32("id", 0);
			materialDTO.Name = dataReader.GetString("name", null);
			materialDTO.Unit = dataReader.GetString("unit", null);
			materialDTO.Description = dataReader.GetString("description", null);
			materialDTO.Pricebyday = dataReader.GetInt32("pricebyday", 0);

			return materialDTO;
		}

		#endregion
	}
}
